package club.ruanx.movie.controller;

import club.ruanx.acl.entity.SysUser;
import club.ruanx.acl.service.SysUserService;
import club.ruanx.bean.PageBean;
import club.ruanx.bean.ResultVO;
import club.ruanx.core.exception.TargetEntityNotFound;
import club.ruanx.movie.common.aop.annotation.FilterSensitiveCommentWords;
import club.ruanx.movie.controller.vo.MovieCommentVO;
import club.ruanx.movie.entity.MovieComment;
import club.ruanx.movie.param.MovieCommentParam;
import club.ruanx.movie.service.MovieCommentService;
import club.ruanx.security.bean.UserAuthInfoContextHolder;
import club.ruanx.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 阮胜
 * @date 2018/11/11 20:34
 */
@RestController
@RequestMapping("/movie/comment")
@Slf4j
public class MovieCommentController {
    private final MovieCommentService movieCommentService;
    private final SysUserService sysUserService;

    public MovieCommentController(MovieCommentService movieCommentService, SysUserService sysUserService) {
        this.movieCommentService = movieCommentService;
        this.sysUserService = sysUserService;
    }


    @PostMapping("/save")
    public MovieComment save(@RequestBody @Valid MovieCommentParam movieCommentParam) throws Exception {
        return movieCommentService.saveParam(movieCommentParam);
    }

    @PreAuthorize("hasAnyRole('USER','SUPERADMIN')")
    @PostMapping("/submit")
    @FilterSensitiveCommentWords(key = "#movieCommentParam.content")
    public ResultVO comment(@Valid @RequestBody MovieCommentParam movieCommentParam) throws Exception {
        String uid = UserAuthInfoContextHolder.getCurrentUserAuthInfoThrow().getUid();
        movieCommentParam.setUserId(uid);
        movieCommentService.saveParam(movieCommentParam);
        return ResultVO.builder().success(true).message("评论成功").build();
    }

    @GetMapping("/{movieId}/list")
    public List<MovieCommentVO> comments(@PathVariable String movieId, PageBean pageBean) {
        return movieCommentService.findAllByMovieId(movieId, pageBean).getContent().stream()
                .map(movieComment -> {
                    MovieCommentVO vo = new MovieCommentVO();
                    BeanUtils.copyPropertiesIgnoreNull(movieComment, vo);
                    try {
                        SysUser sysUser = sysUserService.findById(movieComment.getUserId());
                        vo.setUser(sysUser);
                    } catch (TargetEntityNotFound e) {
                        log.error(e.getMessage());
                    }
                    return vo;
                }).filter(movieCommentVO -> movieCommentVO.getUser() != null)
                .collect(Collectors.toList());
    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable String id) throws Exception {
        movieCommentService.delete(id);
    }

    @DeleteMapping("/delete")
    public List<String> deleteAll(String[] ids) {
        return movieCommentService.deleteIn(ids);
    }
}
